package manage_system.demo02;

import manage_system.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentOperater {

    //增加
    public void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);

        Student student = new Student();

        while (true) {
            System.out.println("请输入要添加的学生编号");
            String id = sc.next();
            int index = index(list, id);
            if (index != -1) {
                System.out.println("输入的编号已存在");
                continue;
            } else {
                //            System.out.println("请输入要添加的学生编号:");
                student.setId(id);

                System.out.println("请输入要添加的学生姓名:");
                student.setName(sc.next());

                System.out.println("请输入要添加的学生年龄:");
                student.setAge(sc.nextInt());
                list.add(student);
                break;
            }
        }


    }


    //修改
    public void alterStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要修改的学生编号");
        int index = index(list, sc.next());
        if (index == -1) {
            System.out.println("要查询的编号不存在");

        } else {
            Student student = list.get(index);
            System.out.println("请输入修改后的姓名");
            student.setName(sc.next());
            System.out.println("请输入修改后的年龄");
            student.setAge(sc.nextInt());
            System.out.println("修改成功");

        }
    }

    //查询所有
    public void queryAll(ArrayList<Student> list) {
        //||list.isEmpty()
        if(list.size()==0){
            System.out.println("没有数据,请添加后再试");
            return;
        }
        System.out.println("编号\t\t名字\t\t年龄");
        for (int i = 0; i < list.size(); i++) {
            Student st = list.get(i);
            System.out.println(st.getId() + "\t\t" + st.getName() + "\t\t" + st.getAge());
        }

    }



    //删除
    public void remove(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要删除的学生编号");
        int index = index(list, sc.next());
        if (index == -1) {
            System.out.println("要删除的的编号不存在");

        } else {
            Student student = list.get(index);
            //删除成功并显示删除的学生编号和姓名
            System.out.println(list.get(index).getId() + "\t" + list.get(index).getName() + "删除成功");
            list.remove(index);
        }
    }

    // 查询学生的ID在集合中是否存在,不存在返回-1,存在则发挥集合中的索引值
    public int index(ArrayList<Student> list, String id) {
        //如果集合长度为0则直接返回false
        //如果不为空,则遍历集合
        //
        if (list.size() == 0) {
            return -1;
        } else {
            for (int i = 0; i < list.size(); i++) {
                if (id.equals(list.get(i).getId())) {
                    return i;
                }
            }
            return -1;
        }

    }

    //查询名字 打印重名的学生信息
    public void sameName(ArrayList<Student> list) {
        ArrayList<Student> studentsSameName = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        String inputName = sc.nextLine();
        for (int i = 0; i < list.size(); i++) {
            if (inputName.equals(list.get(i).getName())) {
                studentsSameName.add(list.get(i));
            }
        }
        if (studentsSameName.size() == 0) {
            System.out.println("没有找到姓名是" + inputName + "的学生");
        } else {

            System.out.println("编号\t\t名字\t\t年龄");
            for (int i = 0; i < studentsSameName.size(); i++) {
                Student st = studentsSameName.get(i);
                System.out.println(st.getId() + "\t\t" + st.getName() + "\t\t" + st.getAge());

            }
        }


    }
}
